import axios from 'axios'

const baseURL = 'http://localhost:8888/api/private/v1/'
    // const baseURL = 'http://192.168.18.22:8888/api/private/v1/'
    // const baseURL = 'http://www.lovegf.cn:8888/api/private/v1/'
axios.defaults.baseURL = baseURL

//拦截器 检查你是否登录 不能阻止没有登录看别的页面 只是传递你的登陆信息
axios.interceptors.request.use(function(config) {
    // 处理请求之前的配置
    let token = localStorage.getItem('mytoken');
    // console.log(config, 666);
    if (token) {
        config.headers["Authorization"] = token
    }
    return config;
}, function(error) {
    // 请求失败的处理
    return Promise.reject(error);
});





//登录验证
export const user = params => {
    return axios.post('login', params).then(res => res.data)
};
//获取用户
export const getlist = params => {
        return axios.get('users', params).then(res => res.data)
    }
    //改变用户状态
export const changestate = params => {
    return axios.put(`users/${params.uId}/state/${params.type}`).then(res => res.data)
}

//添加用户
export const adduser = params => {
    return axios.post('users', params).then(res => res.data)
}

//根据id 获取用户
export const getuserbyid = params => {
        return axios.get(`users/${params.id}`).then(res => res.data)
    }
    //编辑用户
export const edituser = params => {
    return axios.put(`users/${params.id}`, params).then(res => res.data)
}

//删除用户
export const deleuser = params => {
    return axios.delete(`users/${params}`).then(res => res.data)
}

//获取角色
export const getroles = params => {
    return axios.get('roles').then(res => res.data)
}

//角色分配
export const grantroles = params => {
    return axios.put(`users/${params.id}/role`, { id: params.id, rid: params.rid }).then(res => res.data)
}

//获取权限列表
export const getrightlist = params => {
    return axios.get(`rights/${params.type}`).then(res => res.data)
}

//删除指定权限
export const deleteroles = params => {
    return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`).then(res => res.data)
}

//角色授权
// export const grantlist = (params) => {
//     return axios.post(`roles/${params.roleId}/rights`, { roleId: params.id, rids: params.rids }).then(res => res.data)
// }
export const grantlist = (roleId, rids) => {
    return axios.post(`roles/${roleId}/rights`, rids).then(res => res.data)
}

//左边分类权限
export const getmenus = () => {
    return axios.get("menus").then(res => res.data)
}

//商品分类
export const getcate = (params) => {
    return axios.get('categories', { params: params }).then(res => res.data)
}